﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.Models;
using zhangmenren.IService;
using Newtonsoft.Json;

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class ClassifyController : Controller
    {
        private IClassMSGService classMSGService;

        public ClassifyController(IClassMSGService _classMSGService)
        {
            classMSGService = _classMSGService;
        }
        /// <summary>
        /// 后台分类管理
        /// </summary>
        /// <returns>The index.</returns>
        public IActionResult Index()
        {
            return View();
        }


        public async Task<string> GetClass(int page = 1)
        {
            int total = 0;
            IEnumerable<ClassifyEntity> dataList = null;
            dataList = await classMSGService.Select();
            if (dataList.Any())
            {
                total = dataList.Count();
            }
            return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

        }

        public IActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Add(ClassifyEntity entity)
        {
            if (entity != null)
            {
                var result = classMSGService.Add(entity);
                return Json(new { State = "OK", Msg = "添加成功" });
            }
            return Json(new { State = "FAIL", Msg = "添加错误，请稍后重试。" });
        }


        /// <summary>
        /// 删除banner图
        /// </summary>
        /// <returns>The del.</returns>
        /// <param name="id">Identifier.</param>
        [HttpPost]
        public JsonResult Del(int id = 0)
        {
            if (id != 0)
            {
                var result = classMSGService.Del(id);
                return Json(new { state = "ok", msg = "删除成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }

        /// <summary>
        /// 更新banner状态
        /// </summary>
        /// <returns>The enabled.</returns>
        /// <param name="isEnabled">If set to <c>true</c> is enabled.</param>
        /// <param name="id">Identifier.</param>
        [HttpPost]
        public JsonResult IsEnabled(bool isEnabled, int id = 0)
        {
            if (id != 0)
            {
                var result = classMSGService.EditState(id, isEnabled);
                return Json(new { state = "ok", msg = "修改成功" });
            }
            return Json(new { state = "fail", msg = "标识错误" });
        }


    }
}
